<template>
  <view class="address">
    <!-- fixed 定位 -->
    <view class="add-address" @click="openNewAddressPoup">新增收货地址</view>

    <view
      class="address-card"
      v-for="(item ,index) in addressList"
      :key="index"
      @click="chooseAddress(item)"
    >
      <view>
        <view class="info-one">
          <u-tag text="默认" type="success" size="mini" v-show="item.addressDefault" />
          <view>{{ item.addressUserName }}</view>
          <!-- <view>先生</view> -->
          <view>{{ item.addressPhone }}</view>
        </view>
        <view class="info-one">
          <u-tag :text="item.addressTag" type="primary" size="mini" :style="{marginRight:'5px'}" />
          <view
            class="card-address u-line-1"
          >{{ item.elifeSchool.schoolName }} {{ item.elifeBuild.buildName }} {{ item.addressRoomId }}</view>
        </view>
      </view>

      <view
        class="custom-icon-bianji custom-icon"
        style="color:#919293;"
        @click.stop="editAddress(item)"
      ></view>
    </view>

    <!-- 新增收货地址弹窗 -->
    <new-address ref="newAddress"></new-address>
  </view>
</template> 
<script>
import NewAddress from "./components/newAddress";
import { formatData }  from "@/common/js/utils";

export default {
  components: {
    NewAddress,
  },
  data() {
    return {
      addressList: [],
      isTakeBox: false,  //取件标志 代表是通过 首页进来的
    };
  },
  onReady() {
    this.getAllAddressList();
  },
  onLoad(option) {
    console.log(option);
    //当 option传 type = takeBox 的时候代表下单的时候传过来的
    if (option.hasOwnProperty("type") && option.type === "takeBox") {
      //取件 标志
      this.isTakeBox = true;
    }
  },
  methods: {
    // 打开新增地址弹窗
    openNewAddressPoup() {
      //代表是 新增
      this.$refs.newAddress.poupTitle = "新增收货地址";
      this.$refs.newAddress.form.isEdit = false;

      // 测试
      this.$u.api.getLinkageInfoApi().then((res) => {
        // 数据格式化处理
        var formatD = formatData(res.data);
        this.$refs.newAddress.list = formatD;
        this.$refs.newAddress.show = true;
      });
    },
    compare(p) {
      //这是比较函数  未了将默认收货地址放到第一位
      return function (m, n) {
        var a = m[p];
        var b = n[p];
        return b - a; //升序
      };
    },
    // 获取所有的收获信息
    getAllAddressList() {
      this.$u.api.getAllAddressApi().then((res) => {
        // 需要将 默认地址 放到第一位
        this.addressList = res.data;
        this.addressList.sort(this.compare("addressDefault"));
      });
    },
    //跳转路由且
    chooseAddress(addressInfo) {
      // // 获取当前页上的栈（数组形式）
      // var pages = getCurrentPages();
      // //上一页面
      // var prevPage = pages[pages.length - 2]; // （pages.length - 3 上上页 pages.length - 1当前页，以此类推）
      // prevPage.setData({
      //   //直接给上一个页面赋值
      //   chooseName: "1111111111111111", // 注意： name必须是A页面data定义过得字段
      // });
      // console.log(getCurrentPages());
      console.log(addressInfo)
      this.$u.vuex("vuex_chooseAddress",addressInfo)
      // 返回上一页
      uni.navigateBack({ delta: 1 });
    },
    //编辑收货地址
    editAddress(Info) {
      console.log(Info);
      var form = {
        addressId:"",
        name: "",
        phone: "",
        room:'',
        schoolBuildRoom: "", //仅存拼接好以后的字符串
        schoolBuildRoomList: [], //存获取到的list  list[0]学校 list[1]楼栋 list[2]寝室
        defaultAddress: false,
      };
      form.name = Info.addressUserName;
      form.addressId = Info.addressId;
      form.phone = Info.addressPhone;
      form.room = Info.addressRoomId;
      //是否是默认地址
      if (Info.addressDefault) {
        form.defaultAddress = true;
      }
      //便利获取数据 学校 楼栋 宿舍
      //学校
      var school = {
        value: Info.elifeSchool.schoolId,
        label: Info.elifeSchool.schoolName,
      };
      form.schoolBuildRoomList.push(school);
      form.schoolBuildRoom += Info.elifeSchool.schoolName;
      //楼栋
      var build = {
        value: Info.elifeBuild.buildId,
        label: Info.elifeBuild.buildName,
      };
      form.schoolBuildRoomList.push(build);
      form.schoolBuildRoom += "/" + Info.elifeBuild.buildName;
      // //宿舍
      // var room = {
      //   value: Info.elifeRoom.roomId,
      //   label: Info.elifeRoom.roomName,
      // };
      // form.schoolBuildRoomList.push(room);
      // form.schoolBuildRoom += "/" + Info.elifeRoom.roomName;
      // console.log(form);
      this.$refs.newAddress.form = form;
      //代表是 编辑
      this.$refs.newAddress.poupTitle = "编辑收货地址";
      this.$refs.newAddress.form.isEdit = true;

      //获取三级联动数据
      // this.openNewAddressPoup();
      this.$u.api.getLinkageInfoApi().then((res) => {
        // 数据格式化处理
        var formatD = formatData(res.data);
        this.$refs.newAddress.list = formatD;
        this.$refs.newAddress.show = true;
      });
    },
  },
};
</script>

<style scoped lang ='less' >
.address {
  width: 100vw;
  height: 100vh;
  background-color: #f1f8fa;
  & > view:last-child{
    margin-bottom: 100px;
  }
  .add-address {
    position: fixed;
    width: 100vw;
    line-height: 80px;
    color: #ffffff;
    text-align: center;
    font-size: 18px;
    bottom: 0;
    background-color: #adb838;
    height: 80px;
  }
  .address-card {
    height: 70px;
    background-color: #ffffff;
    border-bottom: 1px solid #ebeef5;
    padding: 5px 10px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    .info-one {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
      font-size: 15px;
      & > view {
        margin-right: 5px;
      }
      .card-address {
        color: #919293;
        font-size: 13px;
      }
    }
  }
}
</style>
